// pages/order/allOrder/allOrder.js
//const app = getApp();
var api = require("../../../api.js"),
    app = getApp();
var handler=require("../../../utils/function.js");
Page({
    /**
     * 页面的初始数据
     */
    data: {
        img: handler.userImg,
        order: null,
        page:1,
        order_url: [
          "/pages/driverOrder/orderedStatus/orderedStatus",
          "/pages/driverOrder/orderGoing/orderGoing",
          "/pages/driverOrder/bill/bill",
          "/pages/driverOrder/orderDetail/orderDetail",
          "/pages/driverOrder/orderedStatus/orderedStatus",
        ],
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
      // 获取优惠券
      if (options.uid) {
        app.shareCouponsMessage(options.uid)
      }
    },
  // intoPageHandler 页面跳转
  intoPageHandler: function (e) {
    if (e.currentTarget.dataset.start == 4) {
      handler.intoPageHandler(e.currentTarget.dataset.skip + '?orderstart=1&order_no=' + e.currentTarget.dataset.order_no);
      return false;
    }
    handler.intoPageHandler(e.currentTarget.dataset.skip + '?order_no=' + e.currentTarget.dataset.order_no)
  },
    // 进入订单详情页
    goOrderDetail: function(e) {
      var id=e.currentTarget.dataset.id;
      var index = e.currentTarget.dataset.index;
      const {order}=this.data;
      //未接单
      if (order[index].is_send == 1 && order[index].initiate == 1 && order[index].is_pay == 1){
        wx.navigateTo({
          url: '/pages/userOrder/orderDetail/orderDetail?order_no=' + e.currentTarget.dataset.id,
        })
      }
      else if (order[index].is_send == 0 && order[index].apply_delete == 0) {
        wx.navigateTo({
          url: '/pages/map/orderAndDriverGoing/orderAndDriverGoing?order_no=' + e.currentTarget.dataset.id,
        })
      }else if (order[index].apply_delete == 1) {
        wx.navigateTo({
          url: '../orderDetail/orderDetail?order_no=' + e.currentTarget.dataset.id,
        })
      }else if (order[index].apply_delete == 0 && order[index].is_service==0){
        wx.navigateTo({
          url: '../../map/getOrderNotGoing/getOrderNotGoing?order_no=' + e.currentTarget.dataset.id,
        })
      } else if (order[index].apply_delete == 0 && order[index].is_service == 1) {
        wx.navigateTo({
          url: '../../map/orderGoing/orderGoing?order_no=' + e.currentTarget.dataset.id,
        })
      } else if (order[index].apply_delete == 0 && order[index].is_service == 2) {
        wx.navigateTo({
          url: '/pages/pay/paidOrder/paidOrder?order_no=' + e.currentTarget.dataset.id,
        })
      }else{
        wx.navigateTo({
          url: '/pages/userOrder/orderDetail/orderDetail?order_no=' + e.currentTarget.dataset.id,
        })
      }
    },
    deleteOrderHandler: function() {
        const that = this;
        wx.showModal({
            title: '删除订单',
            content: '确认是否删除所有订单',
            success: function(res) {
                if (res.confirm) {
                    that.setData({
                        order: []
                    });
                }
            },
            fail: function() {},
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
      console.log(this.data.page)
        var that = this;
        app.request({
          url: api.order_branch.getTodayOrder,
            method: 'post',
            data: {},
            success: function(rs) {
                if (rs.code == 1) {
                    that.setData({
                        order: rs.data.data
                    });
                } else {}
            }
        })
    },
    //下来刷新
    // onPullDownRefresh: function() {
    //     var that = this;
    //     var { order}=this.data;
    //     wx.showLoading({});
    //     app.request({
    //         url: api.order.order_list,
    //         method: 'post',
    //         data: {
    //         },
    //         success: function(rs) {
    //             wx.hideLoading()
                
    //           if (rs.code == 1) {
    //             if (rs.data.data.length != 0) {
    //               order.push(rs.data.data)
    //               that.setData({
    //                 order
    //               });
    //             }
    //           } else { }
    //         }
    //     })
    // },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        var that = this;
        var { order ,page} = this.data;
        wx.showLoading({});
        var p = page + 1;
        // p = p + 1;
        this.setData({ page:p})
        app.request({
            url: api.order_branch.getTodayOrder,
            method: 'post',
            data: {
                page: p,
            },
            success: function(rs) {
                wx.hideLoading()
                if (rs.code == 1) {
                  if (rs.data.data.length!=0){
                    for (var i = 0; i < rs.data.data.length;i++){
                      order.push(rs.data.data[i])
                    }
                    console.log(rs.data.data,'22222')
                    // order.push(rs.data.data)
                    that.setData({
                      order
                    });
                  }
                } else {}
            }
        })
    },
  // 用户分享
  onShareAppMessage: function () {
    let title = app.shareAppMessage().title;
    let imageUrl = app.shareAppMessage().imageUrl;
    let desc = app.shareAppMessage().desc;
    let path = app.shareAppMessage().path;
    // console.log(app.shareAppMessage())
    return {
      title: title,
      imageUrl: imageUrl,
      desc: desc,
      path: path,
    }
  },
  onHide(){
    this.setData({ page:1})
    console.log(this.data.page)
  },
  onUnload(){
    console.log(this.data.page)
  },
})